<template>
  <div>
    <basic-page-vxe
      ref="basePage"
      list-api="querySysParam"
      del-api="deleteSysParam"
      export-api="exportSysParam"
      :export-name="$route.meta.title"
      :search-form="basicData.searchForm"
      :search-handle="basicData.searchHandle"
      :search-other-data="basicData.searchData"
      :columns="basicData.columns"
    />
    <!--新增-->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="600px">
      <el-form ref="dataForm" :rules="rules" :model="temp" :inline="true" label-position="right" label-width="120px" style="width: 100%">
        <el-form-item label="参数名称" prop="paramName">
          <el-input v-model="temp.paramName" style="width: 360px" placeholder="请输入" maxlength="32" clearable :disabled="dialogStatus === 'update'" />
        </el-form-item>
        <el-form-item label="参数键名" prop="paramKey">
          <el-input v-model="temp.paramKey" style="width: 360px" placeholder="请输入" maxlength="32" clearable :disabled="dialogStatus === 'update'" />
        </el-form-item>
        <el-form-item label="参数键值" prop="paramValue">
          <el-input v-model="temp.paramValue" style="width: 360px" type="textarea" placeholder="请输入" maxlength="32" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSysParam, updateSysParam, getSysDictByType } from '@/api/admin'

export default {
  name: '',
  components: {

  },
  directives: {},
  filters: {},
  data() {
    return {
      basicData: {
        columns: [
          { config: { title: '参数名称', field: 'paramName' }, type: 'copy' },
          { config: { title: '参数键名', field: 'paramKey' }, type: 'copy' },
          { config: { title: '参数键值', field: 'paramValue', width: '100' }, type: 'copy' },
          { config: { title: '操作', field: 'opt', width: '180' }, type: 'render',
            render: (h, scope) => {
              return (
                <div>
                  <el-button plain size='mini' type='primary' v-permission="'update'" onClick={() => this.handleUpdate(scope.row)}><i class='el-icon-edit'></i> 编辑</el-button>
                  <el-button plain size='mini' type='danger' v-permission="'delete'" onClick={() => this.$refs.basePage.handleDelete(scope.row)}><i class='el-icon-delete'></i> 删除</el-button>
                </div>
              )
            }
          }
        ],
        searchData: { dict: 'carbon_dioxide' },
        searchForm: [
          { type: 'Input', label: '参数名称', prop: 'paramName', width: '180px' },
          { type: 'Input', label: '参数键名', prop: 'paramKey', width: '180px' },
          { type: 'Input', label: '参数键值', prop: 'paramValue', width: '180px' }
        ],
        searchHandle: [
          { label: '新增', type: 'success', icon: 'el-icon-plus', permission: 'add', handle: () => { this.handleCreate() } }
        ]

      },

      prjmgtGasOptions: [],
      temp: {
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        paramName: [{ required: true, message: '请输入参数名称！', trigger: ['blur', 'change'] }],
        paramKey: [{ required: true, message: '请输入参数键名！', trigger: ['blur', 'change'] }]
      }
    }
  },
  computed: {},
  created() {
    this.getType()
  },
  mounted() {
  },
  methods: {
    // 业务逻辑
    getType() {
      getSysDictByType('gas_medium').then(response => {
        this.prjmgtGasOptions = response.data
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addSysParam(this.temp).then(response => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '新增成功',
              type: 'success',
              duration: 2000
            })
            this.handleReset()
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          delete tempData.createTime
          updateSysParam(tempData).then(response => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '编辑成功',
              type: 'success',
              duration: 2000
            })
            this.handleReset()
          })
        }
      })
    },
    // 刷新
    handleReset() {
      this.getType()
      this.$refs.basePage.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.warning{
  text-align: center;
  font-size: 20px;
}
.el-icon-info{
  font-size: 30px;
  color: orange;
}
</style>
